using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class HistoryGroupMembersRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class HistoryGroupMembersDbo : DatabaseObject
        {
            public HistoryGroupMembersDbo()
            {
                Name = "HistoryGroupMembers";
                GeneratedClassName = "HistoryGroupMembers";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "HistoryGroupId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "HistoryGroupId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "HistoryId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "HistoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TargetRate", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "TargetRate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TargetSpacesConversion", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "TargetSpacesConversion" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TargetFuelsConversion", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "TargetFuelsConversion" });
              
            }
        }
		
		public static List<HistoryGroupMemberRow> Select(Expression<Func<HistoryGroupMemberRow, bool>> expression)
        {
            List<HistoryGroupMemberRow> rows = new List<HistoryGroupMemberRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<HistoryGroupMemberRow>(new HistoryGroupMembersDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<HistoryGroupMemberRow> Select()
		{
			List<HistoryGroupMemberRow> rows = new List<HistoryGroupMemberRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<HistoryGroupMemberRow>(new HistoryGroupMembersDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<HistoryGroupMemberRow> PagedSelect(Expression<Func<HistoryGroupMemberRow, bool>> filterExpression, Expression<Func<HistoryGroupMemberRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<HistoryGroupMemberRow> rows = new List<HistoryGroupMemberRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<HistoryGroupMemberRow>(new HistoryGroupMembersDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<HistoryGroupMemberRow> HistoryGroupMemberList)
        {
            HistoryGroupMemberRow row;

            while (reader.Read())
            {
                row = new HistoryGroupMemberRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					HistoryGroupId = Util.CastDbValue<int>(reader["HistoryGroupId"]),
					HistoryId = Util.CastDbValue<int>(reader["HistoryId"]),
					TargetRate = Util.CastDbValue<decimal>(reader["TargetRate"]),
					TargetSpacesConversion = Util.CastDbValue<decimal>(reader["TargetSpacesConversion"]),
					TargetFuelsConversion = Util.CastDbValue<decimal>(reader["TargetFuelsConversion"])
                };
                                
                HistoryGroupMemberList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(HistoryGroupMemberRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<HistoryGroupMemberRow>(new HistoryGroupMembersDbo(), row);
            }
        }

		public static void Update(HistoryGroupMemberRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<HistoryGroupMemberRow>(new HistoryGroupMembersDbo(), row);
            }
        }

		public static void Delete(Expression<Func<HistoryGroupMemberRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<HistoryGroupMemberRow>(new HistoryGroupMembersDbo(), expression);
            }
        }

	}
}
	
